package com.deyuanyun.pic.pbf.service.impl;

import com.deyuanyun.pic.common.tool.Globals;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.pbf.controller.dto.DefectInfoDTO;
import com.deyuanyun.pic.pbf.controller.dto.DefectSuggestVO;
import com.deyuanyun.pic.pbf.controller.dto.InspTaskQueryVO;
import com.deyuanyun.pic.pbf.controller.dto.NearbyLocationDTO;
import com.deyuanyun.pic.pbf.domain.DefectInfo;
import com.deyuanyun.pic.pbf.domain.RepairTaskWorkload;
import com.deyuanyun.pic.pbf.mapper.DefectInfoMapper;
import com.deyuanyun.pic.pbf.mapper.DefectSuggestMapper;
import com.deyuanyun.pic.pbf.mapper.repair.RepairTaskExpandMapper;
import com.deyuanyun.pic.pbf.service.DefectInfoService;
import com.deyuanyun.pic.settings.domain.util.DomainCodeInterceptor;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class DefectInfoServiceImpl implements DefectInfoService {
    @Autowired
    private DefectInfoMapper defectInfoMapper;

    @Autowired
    private DefectSuggestMapper defectSuggestMapper;
    
    @Autowired
    private RepairTaskExpandMapper repairTaskExpandMapper;

    @Override
    public DefectInfoDTO queryDefectInfoDTO(String defectInfoId) {

        DefectSuggestVO defectSuggest = new DefectSuggestVO();
        defectSuggest.setDefectInfoId(new String[]{defectInfoId});
        List<DefectInfoDTO> defectInfoList = defectSuggestMapper.queryBySuggestVO(defectSuggest);
        if(defectInfoList == null || defectInfoList.isEmpty()){
            return null;
        }else{
            return defectInfoList.get(0);
        }
    }

    @Override
    public List<NearbyLocationDTO> queryNearbyLocationByMileage(String pipeLineId,BigDecimal mileage) {
        Asserts.assertTrue(StringUtils.isNotEmpty(pipeLineId), "管道编号不能为空");
        Asserts.assertTrue(mileage != null, "绝对距离不能为空");
        List<NearbyLocationDTO> nearbyLocationList = repairTaskExpandMapper.queryNearbyLocationByMileage(pipeLineId, mileage);
        return DomainCodeInterceptor.newProxyInstance(nearbyLocationList);
        
    }

    public DefectInfo selectByPrimaryKey(String id) {
        return this.defectInfoMapper.selectByPrimaryKey(id);
    }

    public int deleteByPrimaryKey(String id) {
        return this.defectInfoMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(DefectInfo record) {
        return this.defectInfoMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(DefectInfo record) {
        return this.defectInfoMapper.updateByPrimaryKey(record);
    }

    public int insert(DefectInfo record) {
        return this.defectInfoMapper.insert(record);
    }

    @Override
    public List<DefectInfoDTO> queryBySuggestVO(DefectSuggestVO defectSuggestVO) {
        if(defectSuggestVO != null) defectSuggestVO.setRepairTaskIdIsNull(true);
        return defectSuggestMapper.queryBySuggestVO(defectSuggestVO);
    }


    @Override
    public RepairTaskWorkload countWorkload(List<String> defectIds) {
        Assert.isTrue(!ObjectUtil.isEmptyAll(defectIds), "请传入缺陷点信息");
        return defectSuggestMapper.countWorkload(defectIds);
    }

    @Override
    public List<LinkedHashMap<String, Object>> queryTaskInfo(InspTaskQueryVO inspTaskQueryVO) {
        return defectSuggestMapper.queryTaskInfo(inspTaskQueryVO);
    }

    @Override
    public List<Map<String, Object>> queryEvaluTaskAtta(String taskId) {
        return defectSuggestMapper.queryEvaluTaskAtta(taskId);
    }

    @Override
    public void queryEvaluTaskAtta(OutputStream os, List<Map<String, Object>> attaDataMap) {
        if(attaDataMap != null) {
            String fileUploadDir = Globals.getProperties("upload_file_dir");
            ZipOutputStream zos = null;
            try {
                zos = new ZipOutputStream(os);
                zos.setEncoding("GBK");
                //zos.setComment("请全部解压后查看");
                int len = 0;
                byte[] buf = new byte[1024];
                for (Map<String, Object> map : attaDataMap) {
                    Object realNameObj = map.get("REALNAME");
                    Object pathObj = map.get("PATH");
                    if(realNameObj != null && pathObj != null) {
                        File file = new File(fileUploadDir + "/" + pathObj.toString());
                        ZipEntry ze = new ZipEntry(realNameObj.toString());
                        ze.setSize(file.length());
                        zos.putNextEntry(ze);
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                        while ((len = bis.read(buf)) > 0) {
                            zos.write(buf, 0, len);
                        }
                        bis.close();
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally{
                try {
                    zos.close();
                } catch (Exception e2) {
                }
            }
        }
    }
}